<link rel="stylesheet" href="{{url_for('static', filename='font-awesome/css/font-awesome.min.css')}}" media="all">
<link rel="stylesheet" href="{{url_for('static', filename='layui/rc/css/layui.css')}}" media="all">
<link rel="stylesheet" href="{{url_for('static', filename='dist/style/admin.css')}}" media="all">

<div class="layui-form" style="padding: 20px 30px 0 0;">
    <form id="form1" onsubmit="return false" action="##" method="post">
        <input type="hidden" name="id" value="{{ data.id }}">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" value="{{ data.username }}" placeholder="请输入用户名" class="layui-input {% if data.id != '0' %}layui-disabled{% endif %}" {% if data.id != '0' %} disabled {% endif %}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="text" name="password" value="" placeholder="请输入密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-inline">
                <input type="text" name="nickname" value="{{ data.nickname }}" placeholder="请输入昵称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="email" value="{{ data.email }}" placeholder="请输入邮箱" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" lay-filter="sex">
            <label class="layui-form-label">选择性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="3" title="保密" {% if data.sex == 3 %}checked{% endif %}>
                <input type="radio" name="sex" value="1" title="男" {% if data.sex == 1 %}checked{% endif %}>
                <input type="radio" name="sex" value="2" title="女" {% if data.sex == 2 %}checked{% endif %}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户文件盘</label>
            <div class="layui-input-inline">
                <input type="text" name="files_disk_id" value="{{ data.files_disk_id }}" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">积分</label>
            <div class="layui-input-inline">
                <input type="text" name="score" value="{{ data.score }}" placeholder="请输入积分" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">群组</label>
            <div class="layui-input-inline">
                <select name="group" placeholder="请选择群组" lay-filter="group">
                     <option value="0">普通会员</option>
                     {% for i in group_list %}
                         <option value="{{ i.id }}" {% if i.id == data.group|int %} selected {% endif %}>{{ i.title }}</option>
                     {% endfor %}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input type="text" name="description" value="{{ data.description }}" placeholder="请输入描述" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-inline">
                <input type="text" name="avatar" id="avatar" value="{{ data.avatar }}" lay-verify="required" placeholder="请上传图片" autocomplete="off" class="layui-input" >
            </div>
            <button style="float: left;" type="button" class="layui-btn" id="upload-avatar">上传图片</button>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="checkbox" name="status" lay-skin="switch" lay-text="正常|禁止" {% if data.status==1 %}checked{% endif %}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button id="form-submit" class="layui-btn">保存</button>
            </div>
        </div>
    </form>
</div>

<script type="application/javascript" src="{{url_for('static', filename='layui/layui.js')}}"></script>
<script>
    layui.use(['form', 'layer', 'upload'], function(){
        var $ = layui.$; //重点处
        var form = layui.form;
        var upload = layui.upload;

        upload.render({
            elem: '#upload-avatar'
            ,url: '/admin/users/upload'
            ,accept: 'images'
            ,acceptMime: 'image/*'
            ,done: function(res){
                if(res.code == "0"){
                    $("#avatar").val(res.data.src);
                }else{
                    layer.msg("上传格式错误！")
                }
            }
        });

        // 确认按钮
        $('#form-submit').on('click', function () {
            username = $("input[name='username']").val();
            password = $("input[name='password']").val();
            nickname = $("input[name='nickname']").val();
            if(!/^[a-zA-Z0-9_-]{4,16}$/.test(username)){
                layer.msg("用户名长度要在4-16位之间，只能是英文和数字组合！");
                return false
            }
            if(password){
                if(!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(password)){
                    layer.msg("密码长度要在6-16位之间，由数字和字母组成！");
                    return false
                }
            }
            if(!nickname){
                layer.msg("昵称不能为空！");
                return false
            }
            $.ajax({
                url: "/admin/users/edit/{{ data['id'] }}"
                ,type: "POST"
                ,dataType: "json"
                ,data: $('#form1').serialize()
                ,success: function (data) {
                    if(data.code==0){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                    }else{
                        layer.msg(data.msg)
                    }
                }
            });
        });
    });
</script>